package com.nba.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.nba.common.exception.CustomException;
import com.nba.common.cache.GlobalCache;
import com.nba.common.controller.BaseController;
import com.nba.common.dao.IJdbcDao;
import com.nba.common.exception.ErrorCode;
import com.nba.common.form.RequestDataForm;
import com.nba.common.form.ResponseDataForm;
import com.nba.common.service.IService;
import com.nba.common.utils.Environment;
import com.nba.common.utils.FileUtils;
import com.nba.common.utils.UserSessionBean;
import com.nba.common.web.httpobjects.MsgResult;
import com.nba.common.web.utils.RequestUtils;
import com.nba.system.cache.MenuCache;
import com.nba.system.cache.RolePrivCache;
import com.nba.system.cache.SysAreaCache;
import com.nba.system.cache.SysDictCache;
import com.nba.system.cache.SysPrivCache;
import com.nba.system.cache.SysUrlCache;
import com.nba.system.cache.UrlExecSqlCache;
import com.nba.system.cache.UrlValidationCache;
import com.nba.system.cache.UserPrivCache;

@Controller
@RequestMapping(value="/")
public class LoginController extends BaseController{

	@Autowired
	private IJdbcDao jdbcDao;
	
	
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/login")
    public void login(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        RequestDataForm requestDataForm = RequestUtils.getRequestDataForm(request);
        requestDataForm.setRequest(request);
        requestDataForm.setResponse(response);
        WebApplicationContext ctx = WebApplicationContextUtils
                .getWebApplicationContext(request.getSession()
                        .getServletContext());
        ResponseDataForm responseDataForm = ((IService) ctx
                .getBean("userLoginService")).service(requestDataForm);
        if(responseDataForm.getResult() == ResponseDataForm.FAULAIE || responseDataForm.getResult() == ResponseDataForm.RAND_FAULAIE) {
            //登录失败
            String path = request.getContextPath(); 
            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/login.jsp?err=" 
            + (responseDataForm.getResult() == ResponseDataForm.FAULAIE ? "1" : "5");
            response.sendRedirect(basePath);
        } else { 
            //登录成功，跳转到新的页面
            String path = request.getContextPath(); 
            String basePath = "";
            String returnUrl=request.getParameter("returnUrl");
            if(StringUtils.isEmpty(returnUrl)){
                basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/topic/toIndex";
                response.sendRedirect(basePath);
            }else{
                response.sendRedirect(returnUrl);
            }
        }
    }
	/**
     * getUserBtn
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/getUserBtn")
    @ResponseBody
    public List<String> getUserBtn(HttpServletRequest request,
            HttpServletResponse response) {
        UserSessionBean user = (UserSessionBean) request.getSession().getAttribute(Environment.SESSION_USER_LOGIN_INFO);
        List<String> btnList =new ArrayList<String>();
        if(user!= null) {
            Map<String, List<String>> map = (Map<String, List<String>>)GlobalCache.getCache(UserPrivCache.class);
            if(map.containsKey(user.getUserId())){
                btnList =  map.get(user.getUserId());
            }
        }
        return btnList;
    }
	/**
	 * 刷新缓存
	 * @param request
	 * @param response
	 * @param cacheType
	 */
	@RequestMapping(value="/refreshCache")
    public void refreshCache(HttpServletRequest request,HttpServletResponse response,String cacheType){
        if("all".equals(cacheType)){
            GlobalCache.refreshCache(UrlValidationCache.class);
            GlobalCache.refreshCache(UrlExecSqlCache.class);
            GlobalCache.refreshCache(UserPrivCache.class);   
            GlobalCache.refreshCache(RolePrivCache.class); 
            GlobalCache.refreshCache(MenuCache.class); 
            GlobalCache.refreshCache(SysPrivCache.class); 
            GlobalCache.refreshCache(SysUrlCache.class); 
            GlobalCache.refreshCache(SysAreaCache.class); 
            GlobalCache.refreshCache(SysDictCache.class); 
        }else{
            if(cacheType != null && !"".equals(cacheType)){
                String[] cacheArr = cacheType.split("\\,");
                for(String ca : cacheArr){
                    if("v".equals(ca)){
                        GlobalCache.refreshCache(UrlValidationCache.class);
                    }else if("e".equals(ca)){
                        GlobalCache.refreshCache(UrlExecSqlCache.class);
                    }else if("p".equals(ca)){
                        
                    }else if("s".equals(ca)){
                        
                    }else if("u".equals(ca)){
                        GlobalCache.refreshCache(UserPrivCache.class);
                    }
                }
            }
        }
    }
}
